package idv.loveloser.mobilemoodle;




import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.ToggleButton;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;

public class AssignmentEditor extends Activity {
	private SharedPreferences sharedPreferences ;
	private SharedPreferences.Editor edit=null;
	private long client;
	private String sessionKey; 
	private Util util;
	private Spinner courseSpinner;
	private long courseId=-1; 
	private List<MdlCourse> courses;
	private SeekBar timedueSeekbar,gradesSeekbar;
	private int timedue=-1;
	private int grades=-1;
	private ToggleButton resubmit;
	private boolean isResubmit;
	private Button assignmentAdd;
	private EditText assignmentName,assignmentDescription;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.assignment_editor);
		courseSpinner= (Spinner) findViewById(R.id.courses_spinners);
		timedueSeekbar=(SeekBar) findViewById(R.id.time_due_seekbar);
		resubmit=(ToggleButton) findViewById(R.id.resubmit_toggleBtn);
		assignmentDescription=(EditText) findViewById(R.id.assignment_description);
		assignmentName = (EditText) findViewById(R.id.assignment_name);
		resubmit.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				isResubmit=isChecked;
			}
		});
		assignmentAdd=(Button) findViewById(R.id.assignment_add);
		assignmentAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Util.addAssignment(client, sessionKey, grades+"", assignmentDescription.getText().toString(), assignmentName.getText().toString(), isResubmit, timedue,courseId);
				finish();
			}
		});
		timedueSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				timedue=progress;
			}
		});
		gradesSeekbar = (SeekBar) findViewById(R.id.grades_seekbar);
		gradesSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				grades=progress;
			}
		});
		sharedPreferences=getApplicationContext().getSharedPreferences("MySettings", MODE_WORLD_WRITEABLE );
		client =sharedPreferences.getLong("client", -1L);
		sessionKey= sharedPreferences.getString("sessionKey", "");
		String allCoursesStr=Util.getAllCourse(client, sessionKey);
		Gson gson=new Gson();
		courses = gson.fromJson(allCoursesStr, new TypeToken<List<MdlCourse>>(){}.getType()); 
		String[] urlSpinnerContent =new String[courses.size()];
		for(int index=0;index<urlSpinnerContent.length;index++){
			MdlCourse course =courses.get(index);
			urlSpinnerContent[index]=course.getFullname();					
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,urlSpinnerContent);
		courseSpinner.setAdapter(adapter);
		courseSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				MdlCourse course = courses.get(position);
				courseId=course.getId();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				courseId=0L;
				
			}


			
		});
	}
}
